package cn.stronglink.asset.module.assetmanage.instock.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.Instock;
import cn.stronglink.asset.model.InstockDetail;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.instock.mapper.InstockMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class InstockService {

	@Autowired
	private InstockMapper instockMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private AssetRecordMapper assetRecordMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Instock> queryList(Page<Instock> page, Map<String, Object> params) {
		// 首先查询出所有上架单
		List<Instock> list =instockMapper.queryList(page,params);
		//再根据每个上架单号查询出所有明细，并且把每个明细的设备型号，所在机柜拼接存放到上架单中。
		for(int i=0;i<list.size();i++){
			Instock instock=list.get(i);
			String modelNames="";
			List<InstockDetail> detailList =instockMapper.queryDetailByParentId(instock.getId());
			if(detailList!=null&&detailList.size()>0){
				if(detailList.size()>1){
					for(InstockDetail detail:detailList){
						modelNames+=detail.getModelName()+"/";
					}
				}else{
					for(InstockDetail detail:detailList){
						modelNames+=detail.getModelName();
					}
				}
			}
			instock.setModelNames(modelNames);
		}
		page.setRecords(list);
		return page;	
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Instock queryInstockById(Long id) {
		return instockMapper.queryInstockById(id);
	}

	public void remove(Long id) {
		try {
			instockMapper.removeDetailByInstockId(id);
			instockMapper.remove(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void edit(Instock instock) {
		try {
			instockMapper.update(instock);
			for(InstockDetail detail :instock.getDetailList()){
				if (detail.getId() == null) {
					detail.setId(IdWorker.getId());
					detail.setStatus(3);
					detail.setAssetId(Long.parseLong(detail.getAssetId_()));
					detail.setCreateBy(WebUtil.getCurrentUser());
					detail.setCreateTime(new Date());
					instockMapper.insertDetail(detail);		
				}else{
					detail.setUpdateTime(new Date());
					detail.setUpdateBy(WebUtil.getCurrentUser());
					instockMapper.updateDetail(detail);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public int create(Instock instock) {
		instock.setStatus(1);
		instock.setCreateBy(WebUtil.getCurrentUser());
		instock.setCreateTime(new Date());
		int count =instockMapper.insert(instock);	
		if(instock.getDetailList()!=null&&instock.getDetailList().size()>0){
			for(InstockDetail detail:instock.getDetailList()){
				detail.setId(IdWorker.getId());
				detail.setStatus(2);
				detail.setAssetId(Long.parseLong(detail.getAssetId_()));
				detail.setInstockId(instock.getId());
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				instockMapper.insertDetail(detail);	
			}
		}
		return count;
	}

	public void confirmInstock(Instock instock) throws Exception {
		instock.setStatus(3);
		instock.setUpdateBy(WebUtil.getCurrentUser());
		instock.setUpdateTime(new Date());
		//更新上架单为已完成
		instockMapper.update(instock);
		if(instock.getDetailList()!=null&&instock.getDetailList().size()>0){
			for(InstockDetail detail:instock.getDetailList()){
				detail.setStatus(1);
				detail.setId(Long.parseLong(detail.getDetailId()));
				detail.setUpdateBy(WebUtil.getCurrentUser());
				detail.setUpdateTime(new Date());
				instockMapper.updateDetail(detail);
				//更新资产表状态为已在库
				ItAsset asset = new ItAsset();
				asset.setId(Long.parseLong(detail.getAssetId_()));
				asset.setStatus(1);
				itassetMapper.updateItAsset(asset);
					
				 //插入资产生命周期表记录
				 AssetRecord re = new AssetRecord();
				 re.setId(IdWorker.getId());
				 re.setAssetId(Long.parseLong(detail.getAssetId_()));
				 re.setType(9);
				 re.setCreateBy(WebUtil.getCurrentUser());
				 re.setCreateTime(new Date());
				 assetRecordMapper.create(re);
			}
		}
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<InstockDetail> qryDetailInfoByInstockId(Long id) {
		return instockMapper.qryDetailInfoByInstockId(id);
	}

	public void updateStatus(Long id, Integer status) {
		 instockMapper.updateStatus(id,status);
	}

}
